﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace AutoCadConvert
{
    /// <summary>
    /// DwgManager.xaml 的交互逻辑
    /// </summary>
    public partial class DwgManager : UserControl
    {
        private string m_ManagerType = "";
        public string m_TabName = "";
        public DwgManager(string name, string type)
        {
            m_TabName = name;
            m_ManagerType = type;
            this.Initialized += DwgManager_Initialized;
            InitializeComponent();
            VerticalContentAlignment = VerticalAlignment.Top;
            this.Loaded += DwgManager_Loaded;
        }

        private void DwgManager_Loaded(object sender, RoutedEventArgs e)
        {
            //清除标记
            dwgItem.ClearState();
        }

        private void DwgManager_Initialized(object sender, EventArgs e)
        {
            try
            {
                catalog2.ItemsSource = new string[] { "全部" };
                catalog2.Text = "全部";
                catalog2.SelectionChanged += Catalog2_SelectionChanged;
                catalog3.ItemsSource = new string[] { "全部" };
                catalog3.Text = "全部";
                catalog3.SelectionChanged += Catalog3_SelectionChanged;
                catalog4.ItemsSource = new string[] { "全部" };
                catalog4.Text = "全部";
                catalog4.SelectionChanged += Catalog4_SelectionChanged;

                //分类信息
                DisplayCatalog("000000",catalog2);
                //材质信息
                //dwgItem.GetModelMaterial("000000",catalog4);
                //下载文件
                dwgItem.DownDwgFile("000000",m_ManagerType,m_TabName);
                //创建图层
                DwgConvert.CreateDefaultLayer();
            }
            catch(Exception ex)
            {
                System.Diagnostics.Trace.WriteLine(ex.StackTrace);
            }
        }
        public void DisplayCatalog(string typeCode,ComboBox catalog)
        {
            Task.Factory.StartNew(() => { 
                HttpManager.CatalogLevel_Response ret = HttpManager.Catalog_Level(typeCode, m_ManagerType);
                if (ret != null && ret.code == 1001 && ret.data.catalogs!=null)
                {
                    IEnumerable<HttpManager.CatalogLevel_Response.Catalog> lstdata = null;
                    if (m_ManagerType == "type1")
                    {
                        if (m_TabName == "门窗")
                            lstdata = ret.data.catalogs.Where(o => o.name.Contains("门") || o.name.Contains("窗"));
                        else
                            lstdata = ret.data.catalogs.Where(o => !o.name.Contains("门") && !o.name.Contains("窗"));
                    }
                    else if (m_ManagerType == "type")
                    {
                        lstdata = ret.data.catalogs;
                    }
                    this.Dispatcher.BeginInvoke(new Action(() => {
                        catalog.ItemsSource = new string[] { "全部" }.Concat(lstdata.Select(o => o.name));
                        catalog.Tag = new HttpManager.CatalogLevel_Response.Catalog[] { new HttpManager.CatalogLevel_Response.Catalog() { code=typeCode,hasNext=true } }.Concat(lstdata);//绑定分类编号
                        InitCatalogTextBlock(lstdata);
                    }));
                }
            });
        }
        public void ResetRowHeight()
        {
            //textRow.Height = new GridLength(allCatalog.ActualHeight);
        }
        private void SelectChange(ComboBox item, ComboBox child)
        {
            try
            {
                if (item.SelectedIndex == -1) return;
                IEnumerable<HttpManager.CatalogLevel_Response.Catalog> catalogs = item.Tag as IEnumerable<HttpManager.CatalogLevel_Response.Catalog>;
                if (item.SelectedIndex == 0)
                {
                    DisplayCatalog(catalogs.ElementAt(0).code,item);
                    //下载并过滤模型
                    dwgItem.DownDwgFile(catalogs.ElementAt(0).code,m_ManagerType,m_TabName);
                }
                else
                {
                    int idx = item.SelectedIndex;
                    Task.Factory.StartNew(new Action(() =>
                    {
                        HttpManager.CatalogLevel_Response ret = HttpManager.Catalog_Level(catalogs.ElementAt(idx).code, m_ManagerType);
                        if (ret != null && ret.data.catalogs != null)
                        {
                            dwgItem.DownDwgFile(catalogs.ElementAt(idx).code, m_ManagerType, m_TabName);
                            this.Dispatcher.BeginInvoke(new Action(() =>
                            {
                                if (child != null)
                                {
                                    child.ItemsSource = new string[] { "全部" }.Concat(ret.data.catalogs.Select(o => o.name));
                                    child.Text = "全部";
                                    child.Tag = new HttpManager.CatalogLevel_Response.Catalog[] { catalogs.ElementAt(idx) }.Concat(ret.data.catalogs);//绑定分类编号
                                }
                                InitCatalogTextBlock(ret.data.catalogs);
                            }));
                        }
                    }));
                }
            }
            catch(Exception ex)
            {
                DwgConvert.PutMessage(ex.Message);
            }
        }
        private void Catalog4_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            SelectChange(catalog4,null);
        }
        private void Catalog3_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            SelectChange(catalog3, catalog4);
        }
        private void Catalog2_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            SelectChange(catalog2, catalog3);
        }
        private void InitCatalogTextBlock(IEnumerable<HttpManager.CatalogLevel_Response.Catalog> items)
        {
            allCatalog.Children.Clear();
            foreach (HttpManager.CatalogLevel_Response.Catalog item in items)
            {
                TextBlock tb = new TextBlock()
                {
                    Text = item.name,
                    Tag = item,
                    Margin = new Thickness(5),
                };
                tb.MouseEnter += Tb_MouseEnter;
                tb.MouseLeave += Tb_MouseLeave;
                tb.MouseDown += Tb_MouseDown;
                allCatalog.Children.Add(tb);
            }
            ResetRowHeight();
        }
        private void Tb_MouseDown(object sender, MouseButtonEventArgs e)
        {
            try
            {
                //判断是否存在
                if (catalog4.Items.Contains((sender as TextBlock).Text))
                    catalog4.Text = (sender as TextBlock).Text;
                if (catalog3.Items.Contains((sender as TextBlock).Text))
                    catalog3.Text = (sender as TextBlock).Text;
                else if (catalog2.Items.Contains((sender as TextBlock).Text))
                    catalog2.Text = (sender as TextBlock).Text;

                if (sender is TextBlock)
                {
                    HttpManager.CatalogLevel_Response.Catalog catalog = (sender as TextBlock).Tag as HttpManager.CatalogLevel_Response.Catalog;
                    if (catalog.hasNext)
                    {
                        Task.Factory.StartNew(new Action(() =>
                        {
                            HttpManager.CatalogLevel_Response ret = HttpManager.Catalog_Level(catalog.code, m_ManagerType);
                            if (ret != null && ret.data.catalogs != null)
                            {
                                dwgItem.DownDwgFile(catalog.code, m_ManagerType, m_TabName);
                                this.Dispatcher.BeginInvoke(new Action(() =>
                                {
                                    InitCatalogTextBlock(ret.data.catalogs);
                                }));
                            }
                        }));
                    }
                    else
                    {

                        dwgItem.FilterModel(new string[] { catalog.code });
                    }
                }
            }
            catch(Exception ex)
            {
                DwgConvert.PutMessage(ex.Message);
            }
        }
        private void Tb_MouseLeave(object sender, MouseEventArgs e)
        {
            (sender as TextBlock).Foreground = Brushes.Black;
            (sender as TextBlock).FontWeight = FontWeight.FromOpenTypeWeight(400);
        }
        private void Tb_MouseEnter(object sender, MouseEventArgs e)
        {
            (sender as TextBlock).Foreground = Brushes.Blue;
            (sender as TextBlock).FontWeight = FontWeight.FromOpenTypeWeight(800);
        }
        
    }
}